// // 深拷贝(类型考虑/Symbol/Date等的构造/循环优化/共用引用优化等),这里贴2篇文章给大家参考,https://juejin.im/post/5d6aa4f96fb9a06b112ad5b1,https://mp.weixin.qq.com/s/M7KBX3w2KqlWhZFHJSYP6Q)
function deepClone(obj) {
    let result = obj.constructor === Array ? [] : {}
    for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
            if (obj[key] && typeof obj[key] === "object") {
                result[key] = deepClone(obj[key])
            } else {
                result[key] = obj[key]
            }
        }
    }
    return result
}

function clone(obj) {
    let result = obj.constructor === Array ? [] : {}
    for (const key in object) {
        if (Object.hasOwnProperty.call(object, key)) {
            result[key] = obj[key] 
        }
    }
}

// let arr = [1,2,3];
let arr = [{key: 1,name: 2}];
let obj = {key: 1,name: 2}
// for (const [key] of arr) {
//     console.log(key)
// }
// // 与Objec.keys.forEach 比较像
// for (const key in object) {
//     if (Object.hasOwnProperty.call(object, key)) {
//         const element = object[key];
        
//     }
// }


// 计算一个元素所占内存大小
const seen = new WeakMap();

function sizeOfObj(obj) {
    if (obj == null) return 0
    let result = 0;
    const properties = Object.keys(obj)
    for (let i = 0; i < properties.length; i++) {
        const key = properties[i];
        if (typeof obj[key] == 'object' && obj[key] != null) {
            // 可能有重复的 相同指针
            if (seen.has(obj[key])) {
                continue
            }
            seen.add(obj[key])
        }
        result += calculator(key)
        result += calculator(obj[key])
    }
    return result
};

function calculator(obj) {
    const objType = typeof obj;
    switch (objType) {
        case "string":
            return obj.length
        case "boolean":
            return 4
        case "number":
            return 8
        case "object":
            if(Array.isArray(obj)){
                return obj.map(calculator).reduce((acc,curr)=>{
                    return acc + curr
                })
            }else {
                return sizeOfObj(obj)
            }

        default:
            break;
    }
}

// create 实现
Object.myCreate = function (obj) {
    let fn = function () { }
    fn.prototype = obj;
    return new fn()
}
